package com.uhb.base.controller;

import com.uhb.base.util.CommonFunctionUtil;
import org.apache.log4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
	protected Logger logger = Logger.getLogger(this.getClass());
	
	protected ModelAndView getJsonView() {
		ModelAndView mv = new ModelAndView();
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		mv.setView(view);
		return mv.addObject("requestName", this.getRequestName());
	}

	protected ModelAndView getJsonViewFail(int code) {
		ModelAndView mv = new ModelAndView();
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		mv.setView(view);
		return mv;
	}

	protected ModelAndView getJsonViewSuccess() {
		ModelAndView mv = new ModelAndView();
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		mv.setView(view);
		return mv;
	}

	protected ModelAndView getJsonViewSuccess(int length) {
		ModelAndView mv = new ModelAndView();
		MappingJackson2JsonView view = new MappingJackson2JsonView();
		mv.setView(view);
		return mv.addObject("code", length);
	}

	private String getRequestName() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request.getRequestURI();
	}
	
	protected boolean checkAuth(String apt,String key,String scrit){
		String readKey = CommonFunctionUtil.createAuthkey(apt, scrit);
		return key.equals(readKey);
	}
	
	protected boolean isSelect(String sql){
		return sql.trim().substring(0,6).equalsIgnoreCase("select");
	}
	
	
}
